<template>
  <div>
    <el-form @submit.native.prevent :inline="true">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="主本名称"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <select-farm-org
          v-model="params['org_class.eq']"
          placeholder="主体类型"
          clearable
          @change="getData()"
          class="form-control-ml"
        ></select-farm-org>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="startDate"
          placeholder="申请起始时间"
          class="form-control-ml"
          @change="dateChange"
          type="date"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="endDate"
          placeholder="申请截止时间"
          class="form-control-ml"
          @change="dateChange"
          type="date"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      v-loading="loading"
      ref="table"
      name="table"
      class="table-sm"
      :columns="tableColumns"
      :data="tableData"
    >
      <template v-slot:recomStatus="scope">
        <span class="badge" :class="`badge-${scope.row.statusStyle}`">
          {{ scope.row.statusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <view-dialog
      :visible.sync="viewVisible"
      :is-class="isClass"
      :query-id="queryId"
      :relatedId="relatedId"
    ></view-dialog>

    <register-dialog
      :visible.sync="editVisible"
      :query-id="tableId"
      @success="getData()"
    ></register-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

import ViewDialog from '@/gov/views/bus/register/components/ViewDialog'
import SelectAddress from '@/components/select/SelectAddress'
import SelectFarmOrg from '@/components/select/SelectFarmOrg'
import RegisterDialog from '@/gov/views/bus/register/components/RegisterDialog'

export const enumApprovalStatus = {
  0: '未放款',
  1: '审批中',
  2: '待登记',
  3: '已放款',
}

export const enumApprovalStyle = {
  0: 'info',
  1: 'warning',
  2: 'primary',
  3: 'success',
}
export default {
  components: {
    ViewDialog,
    SelectAddress,
    SelectFarmOrg,
    RegisterDialog,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 15,
        customer_name: '',
        'recommend_status.eq': 1,
        'submit_type.eq': 1,
      },
      order: {
        create_time: 'desc',
      },
      loading: false,
      totalCount: 0,
      tableData: [],
      tableColumns: [
        { attrs: { label: '主体名称', prop: 'customer_name' } },
        { attrs: { label: '经营地址', prop: 'address' } },
        { attrs: { label: '主体类型', prop: 'org_class_name' } },
        { attrs: { label: '联系电话', prop: 'mobile_no' } },
        { attrs: { label: '证件号', prop: 'idCard' } },
        { attrs: { label: '申请金额(万元)', prop: 'apply_funds' } },
        { attrs: { label: '申请时间', prop: 'createTime', width: '100px' } },
        { attrs: { label: '资金用途', prop: 'funds_use_dir' } },
        { attrs: { label: '推荐时间', prop: 'recommend_time', width: 90 } },
        { attrs: { label: '推荐人', prop: 'recommend_person' } },
        {
          attrs: { label: '项目状态', prop: 'statusLabel', align: 'center' },
          slot: 'recomStatus',
        },
        {
          attrs: { label: '操作', align: 'center', width: 100 },
          operations: [
            {
              content: '放款登记',
              icon: 'fa fa-legal',
              event: this.handleEdit,
              ifRender: row => +row.is_approval === 0 && +row.confuse_status !== 3,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],
      queryId: '',
      relatedId: '',
      isClass: false,
      viewVisible: false,
      startDate: '',
      endDate: '',

      tableId: '',
      editVisible: false,
    }
  },
  computed: {
    ...mapGetters(['userId', 'user', 'job']),
    isCorp() {
      return this.job === 'org_corp'
    },
    queryAddressCode() {
      return this.user.queryAddressCode || this.user.addressCode
    },
  },
  watch: {},
  created() {
    this.params.address_code = this.queryAddressCode.replace(/0+$/g, '')
    this.getData()
  },
  mounted() {},
  methods: {
    handleEdit(row = {}) {
      this.tableId = row.customer_related_id
      this.editVisible = true
    },
    dateChange() {
      this.params['create_time.gte'] = this.startDate || ''
      this.params['create_time.lte'] = this.endDate ? this.endDate + ' 23:59:59' : ''
      this.getData()
    },
    getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData({
          table: 'customer_related',
          params: this.params,
          order: this.order,
        })
        .then(res => {
          const data = res?.data ?? {}
          this.tableData = (data.data ?? []).map(this.dataAdapter)
          this.totalCount = data.totalCount ?? 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.createTime = this.$utils.dateFormat(item.create_time, 'yyyy-MM-dd')
      item.statusLabel =
        +item.confuse_status === 3 ? '已拒绝' : enumApprovalStatus[item.loan_approval_status]
      item.statusStyle =
        +item.confuse_status === 3 ? 'danger' : enumApprovalStyle[item.loan_approval_status]
      item.idCard = +item.customer_type === 1 ? item.register_no : item.lender_idcard
      return item
    },
    handleView(row = {}) {
      this.queryId = +row.credit_type === 1 ? row.credit_industrial_id : row.credit_direct_id
      this.relatedId = row.customer_related_id
      this.isClass = +row.credit_type === 1
      this.viewVisible = true
    },
  },
}
</script>

<style lang="scss" scoped></style>
